using System.Collections.Concurrent;
using Client.Base;
using UnityEngine;
namespace Client.Hotfix
{
    [UGFM(UGFMWeight.AudioService)]
    public class AudioService : IAwake, IDestroy
    {
        private AudioSource AudioSource_BG;
        private AudioSource AudioSource_Effect;
        private AudioSource AudioSource_Other;
        private readonly ConcurrentDictionary<string, AudioClip> effectClips = new();
        private readonly ConcurrentDictionary<string, AudioClip> bgClips = new();
        private readonly ConcurrentDictionary<string, AudioClip> otherClips = new();
        private ResourceService resourceService;

        public bool enableUISound;
        public bool enableBGSound;


        public void Awake()
        {
            resourceService = UGF.Instance.Resolve<ResourceService>();

            GameObject clone = new GameObject("AudioSource_BG");
            clone.transform.SetParent(UniApp.Instance.transform);
            AudioSource_BG = clone.AddComponent<AudioSource>();

            clone = new GameObject("AudioSource_Effect");
            clone.transform.SetParent(UniApp.Instance.transform);
            AudioSource_Effect = clone.AddComponent<AudioSource>();

            clone = new GameObject("AudioSource_Other");
            clone.transform.SetParent(UniApp.Instance.transform);
            AudioSource_Other = clone.AddComponent<AudioSource>();

            AudioSource_BG.playOnAwake = false;
            AudioSource_BG.loop = false;

            AudioSource_Effect.playOnAwake = false;
            AudioSource_Effect.loop = false;

            AudioSource_Other.playOnAwake = false;
            AudioSource_Other.loop = false;




            this.Log("[ Service ] - SoundService is running...");
        }

        public async void PlayEffectSound(string clipName, float volume = 1.0f)
        {
            if (!enableUISound) return;

            AudioClip clip = effectClips.GetOrAdd(clipName, await resourceService.LoadAudioClipAsync(clipName));

            AudioSource_Effect.volume = volume;
            AudioSource_Effect.clip = clip;
            AudioSource_Effect.Play();
        }

        public async void PlayOtherSound(string clipName, float volume = 1.0f, bool loop = false)
        {
            if (!enableUISound) return;

            AudioClip clip = otherClips.GetOrAdd(clipName, await resourceService.LoadAudioClipAsync(clipName));
            AudioSource_Other.loop = loop;
            AudioSource_Other.volume = volume;
            AudioSource_Other.clip = clip;
            AudioSource_Other.Play();
        }

        public async void PlayBGSound(string clipName, float volume = 1.0f, bool loop = true)
        {
            if (!enableBGSound) return;

            AudioClip clip = bgClips.GetOrAdd(clipName, await resourceService.LoadAudioClipAsync(clipName));
            AudioSource_BG.loop = loop;
            AudioSource_BG.volume = volume;
            AudioSource_BG.clip = clip;
            AudioSource_BG.Play();
        }

        public void Destroy()
        {
            effectClips.Clear();
            bgClips.Clear();
            otherClips.Clear();

        }
    }
}

